﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_HealthVisitingPlanSrhForm
 * 类型：       控制层
 * 功能说明：   健康管家-家访计划列表页面
 * 作者：       黄鑫
 * 创建日期：   2007/09/12
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using log4net;
using Shpm.App.Commons.Utils;
using Shpm.App.Search.Dao;
using Shpm.App.Search.Bean;

public partial class App_Input_HealthVisitingPlanSrhForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_HealthVisitingPlanSrhForm));

    // gridview 家访详细
    private const int COLUMN_DETAIL = 11;

    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(CommonAjaxUtils), this.Page);
        if (!Page.IsPostBack)
        {
            // 取得当前登录的医生id信息
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
            string emplyJob = mstSysUserBean == null ? "" : mstEmpBean.EmployeeJob;
            string emplyLevel = mstSysUserBean == null ? "" : mstEmpBean.EmployeeLevel;
            string emplyRole = mstEmpBean == null ? "" : mstEmpBean.Role;
            ViewState["DOCTOR_ID"] = emplyId;
            ViewState["EMPLY_LEVEL"] = emplyLevel;


            setSerch1();

            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                viewSrhRst.PageIndex = Convert.ToInt32(Context.Items["ParamPageIndex"]);
            }
            if (Context.Items.Contains("MemberNo"))
            {
                this.ViewState["MemberNo"] = Convert.ToString(Context.Items["MemberNo"]);
            }
            if (Context.Items.Contains("sDate"))
            {
                this.ViewState["sDate"] = Convert.ToString(Context.Items["sDate"]);
            }
            if (Context.Items.Contains("eDate"))
            {
                this.ViewState["eDate"] = Convert.ToString(Context.Items["eDate"]);
            }
            if (Context.Items.Contains("Sex"))
            {
                this.ViewState["Sex"] = Convert.ToString(Context.Items["Sex"]);
                this.sex.SelectedValue = Convert.ToString(this.ViewState["Sex"]);
            }
            if (Context.Items.Contains("BuildingNo"))
            {
                this.ViewState["BuildingNo"] = Convert.ToString(Context.Items["BuildingNo"]);
                this.dropBuildNo.SelectedValue = Convert.ToString(this.ViewState["BuildingNo"]);
            }
            if (Context.Items.Contains("FloorNo"))
            {
                this.ViewState["FloorNo"] = Convert.ToString(Context.Items["FloorNo"]);
                setSerch(Convert.ToString(this.ViewState["BuildingNo"]), Convert.ToString(this.ViewState["FloorNo"]), "");
            }
            if (Context.Items.Contains("Room"))
            {
                this.ViewState["Room"] = Convert.ToString(Context.Items["Room"]);
                setSerch(Convert.ToString(this.ViewState["BuildingNo"]), Convert.ToString(this.ViewState["FloorNo"]), Convert.ToString(this.ViewState["Room"]));
            }
        }
    }

    public void setSerch1()
    {
        HealthVisitPlanDao dao1 = new HealthVisitPlanDao();
        CommonDictionaryBean bean = new CommonDictionaryBean();

        //数据词典加载
        bean.Catalog = "SEX";
        IList list1 = dao1.FindByCatalog(bean);
        list1.RemoveAt(0);
        list1.Insert(0, new CommonDictionaryBean());
        sex.DataSource = list1;
        sex.DataBind();

        SalesInfoDao dao = new SalesInfoDao();
        SalesInfoBean salesInfoBean = new SalesInfoBean();
        //楼号邦定
        IList list = dao.FindBuilds();
        if (list != null && list.Count > 0)
        {
            list.Insert(0, salesInfoBean);
        }
        else
        {
            list.Add(salesInfoBean);
        }
        this.dropBuildNo.DataSource = list;
        this.dropBuildNo.DataBind();

        //楼层邦定
        list = new ArrayList();
        list.Add(salesInfoBean);
        this.dropFloor.DataSource = list;
        this.dropFloor.DataBind();

        //房间号邦定
        this.dropRoomNo.DataSource = list;
        this.dropRoomNo.DataBind();
    }
    public void setSerch(string dropBuildNo, string dropFloor, string dropRoomNo)
    {
        SalesInfoDao salesInfoDao = new SalesInfoDao();
        SalesInfoBean salesInfoBean = new SalesInfoBean();
        salesInfoBean.BuildingNo = dropBuildNo;
        //floor
        IList list = salesInfoDao.FindBuildFloor(salesInfoBean);
        if (list != null && list.Count > 0)
        {
            list.Insert(0, salesInfoBean);
        }
        else
        {
            list = new ArrayList();
            list.Add(salesInfoBean);
        }

        this.dropFloor.DataSource = list;
        this.dropFloor.DataBind();
        this.dropFloor.SelectedValue = dropFloor;

        //room
        salesInfoBean.Floor = dropFloor;
        list = salesInfoDao.FindBuildRoom(salesInfoBean);
        if (list != null && list.Count > 0)
        {
            list.Insert(0, salesInfoBean);
        }
        else
        {
            list = new ArrayList();
            list.Add(salesInfoBean);
        }

        this.dropRoomNo.DataSource = list;
        this.dropRoomNo.DataBind();
        this.dropRoomNo.SelectedValue = dropRoomNo;
    }

    // before render
    void Page_PreRender(object sender, EventArgs e)
    {
        // get current doctor responsibility members list
        HealthVisitPlanDao dao = new HealthVisitPlanDao();
        IList lstRst = null;
        HealthVisitPlanBean bean = new HealthVisitPlanBean();
        bean.DoctorId = Convert.ToString(ViewState["DOCTOR_ID"]);
        bean.EmployeeLevel = Convert.ToString(getJobLevel());
        bean.SearchFlg = true;
        bean.EditFlg = false;
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["MemberNo"])))
        {
            bean.MemberNo = Convert.ToString(this.ViewState["MemberNo"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["sDate"])))
        {
            bean.SDate = Convert.ToDateTime(this.ViewState["sDate"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["eDate"])))
        {
            bean.EDate = Convert.ToDateTime(this.ViewState["eDate"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["Sex"])))
        {
            bean.Sex = Convert.ToString(this.ViewState["Sex"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["BuildingNo"])))
        {
            bean.BuildingNo = Convert.ToString(this.ViewState["BuildingNo"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["FloorNo"])))
        {
            bean.FloorNo = Convert.ToInt32(this.ViewState["FloorNo"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["Room"])))
        {
            bean.RoomNo = Convert.ToString(this.ViewState["Room"]);
        }
        lstRst = dao.ListMemberVisit(bean);
        if (lstRst.Count == 0)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0019").ToString();
        }
        else
        {
            viewSrhRst.DataSource = lstRst;
            viewSrhRst.DataBind();
        }
    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }

    // details click
    protected void viewSrhRst_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Request["x"] == null || Request["y"] == null)
            return;

        string visitingId = null;
        string memberNo = null;
        string url = "~/App/Input/HealthVisitingPlanSrhForm.aspx";
        if (e.CommandName == "Plan")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            // Get the Key value from the GridView control.
            DataKey key = viewSrhRst.DataKeys[index];
            memberNo = Convert.ToString(key.Value);
            visitingId  = Convert.ToString(key.Values[1]);
            url = "~/App/Input/HealthVisitPlanUpdForm.aspx";            
        }
        else if (e.CommandName == "Details")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            // Get the Key value from the GridView control.
            DataKey key = viewSrhRst.DataKeys[index];
            memberNo = Convert.ToString(key.Value);
            visitingId = Convert.ToString(key.Values[1]);
            url = "~/App/Input/HealthVisitPlanDetailSrhForm.aspx";           
        }
        // 传递分页编号
        Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;
        Context.Items["VISITING_ID"] = visitingId;
        Context.Items["MEMBER_NO"] = memberNo;
        Context.Items["MemberNo"] = this.ViewState["MemberNo"];
        Context.Items["sDate"] = this.ViewState["sDate"];
        Context.Items["eDate"] = this.ViewState["eDate"];
        Context.Items["Sex"] = this.ViewState["Sex"];
        Context.Items["BuildingNo"] = this.ViewState["BuildingNo"];
        Context.Items["FloorNo"] = this.ViewState["FloorNo"];
        Context.Items["Room"] = this.ViewState["Room"];
        ControllerUtils.Transfer(Context, url, true);    

    }

    // adjust bind data
    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HealthVisitPlanBean bean = (HealthVisitPlanBean)e.Row.DataItem;
            (e.Row.FindControl("hypeMemberNo") as HyperLink).Attributes.Add("onclick", "openPersonDetails('" + bean.MemberNo + "')");

            // has got a plan detail 
            if (string.IsNullOrEmpty(Convert.ToString(((HealthVisitPlanBean)e.Row.DataItem).VisitingId)))
            {
               
            }
        }
    }

    protected void btnSeach_Click(object sender, EventArgs e)
    {
        string floor = Request.Form["ctl00$ContentPlaceHolder1$dropFloor"].ToString();
        string room = Request.Form["ctl00$ContentPlaceHolder1$dropRoomNo"].ToString();

        this.ViewState["MemberNo"] = this.txtMemberNo.Text.Trim();
        this.ViewState["sDate"] = this.txtBeginDate.Text;
        this.ViewState["eDate"] = this.txtEndDate.Text;
        this.ViewState["Sex"] = this.sex.SelectedValue;
        this.ViewState["BuildingNo"] = Convert.ToString(this.dropBuildNo.SelectedValue);
        this.ViewState["FloorNo"] = floor;
        this.ViewState["Room"] = room;

        this.txtMemberNo.Text = "";
        this.txtMemberName.Text = "";
        setSerch(Convert.ToString(this.dropBuildNo.SelectedValue), floor, room);
    }

    // 取得职员权限
    private int getJobLevel()
    {
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
        PersonEmployeeBean beanEmp = new PersonEmployeeBean();
        HealthTherapyDao dao = new HealthTherapyDao();
        beanEmp.EmployeeId = emplyId;
        beanEmp = dao.GetEmployeeJob(beanEmp);
        if (beanEmp == null || string.IsNullOrEmpty(beanEmp.JobLimit))
        { return 0; }
        return Convert.ToInt32(beanEmp.JobLimit);
    }
}
